<template>
  <div>
    <p>[ 98, 21, 34, 20, 12, 32, 56, 1, 4 ]</p>
    <p>{{ newArray }}</p>

  </div>
</template>

<script>
export default {
  name: "",
  data () {
    return {
      array: [98, 21, 34, 20, 12, 32, 56, 1, 4],
      newArray: []
    }
  },
  mounted () {
    this.newArray = this.insert(this.array)
  },
  methods: {
    insert (arr) {
      const n = arr.length
      for (let i = 1; i < n; i++) {
        // 当前需要插入的元素
        let current = arr[i]
        // 从已排序部分的末尾开始比较
        let j = i - 1;
        // 将比 current 大的元素向后移动
        while (j >= 0 && arr[j] > current) {
          arr[j + 1] = arr[j];
          j--;
        }
        // 插入 current 到正确位置
        arr[j + 1] = current
      }
      return arr
    }
  }
}
</script>

<style scoped>

</style>
